function [c,b,p,fout]=match_all_gen2(fpt,ftr,ns_im,n_seeds)

% 0.5414    0.2705    0.2178    1.2005
% profile on
% add data directory
% fpt = strcat('Dewarped\Dataset_5\', fpt);
% ftr = strcat('Dewarped\Dataset_5\', ftr);

f_dir = 'Dewarped\Dataset_1\';
[pts_2d_all,trs_all,cams_all,pts_3d_truth] = read_data(f_dir,n_seeds);

pt = pts_2d_all(ns_im);
pts_2d = pt;
tr = trs_all(ns_im);
trs = tr;
cams = cams_all(ns_im);

% reformating camera parameters
for i = 1:3
  ex(i,1) = cams{i,1}(1);
  ey(i,1) = cams{i,1}(2);
  xo(i,1) = cams{i,1}(3);
  yo(i,1) = cams{i,1}(4);
  focal(i,1) = cams{i,1}(5);
end

N=length(ns_im);
k=0;
for i=1:N-1
    for j=i+1:N
        k=k+1;
        cmb(k,:)=[ns_im(i),ns_im(j)]
    end
end

cmb = [1 2; 2 3; 3 1];

p=zeros(3,n_seeds);

c = cell(3,1);
c{1} = generate_2img_cost_matrix(pts_2d(1:3),trs,ex,ey,xo,yo,focal);
c{2} = generate_2img_cost_matrix({pts_2d{2};pts_2d{3};pts_2d{1}},{trs{2};trs{3};trs{1}},[ex(2);ex(3);ex(1)],[ey(2);ey(3);ey(1)],[xo(2);xo(3);xo(1)],[yo(2);yo(3);yo(1)],[focal(2);focal(3);focal(1)]);
c{3} = generate_2img_cost_matrix({pts_2d{3};pts_2d{1};pts_2d{2}},{trs{3};trs{1};trs{2}},[ex(3);ex(1);ex(2)],[ey(3);ey(1);ey(2)],[xo(3);xo(1);xo(2)],[yo(3);yo(1);yo(2)],[focal(3);focal(1);focal(2)]);

for i=1:k
    i
    [cost,b]=match_def2(c{i},cmb(i,:),n_seeds);
    rand('state',100);
    f=match_rand_flow(n_seeds);
    [f,n_c]=match_cycle(f,b,cost);
    fout{i}=match_flow2_proc(f,n_seeds);
%     fout{i}
%     p=p+match_recon_gen_new(fpt,ftr,cmb(i,:),n_seeds,fout);
end
% p=p/k;

f1 = [];
for i = 1:length(fout)
    f1 = [f1; fout{i}];
end

f1

res = find_matching_loops(fout, N, n_seeds);

% for i = 1:length(res)
%     res{i}
% end

[n_t,tmp] = size(res);

% c{1}
% for i = 1:length(res)
%     res{i}
% end
% size(res)
% 
% n_seeds
mm = zeros(n_seeds,N+1);
cc = 0;

% c = {};
f = focal;
% c{1} = generate_2img_cost_matrix(pts_2d(1:3),trs,ex,ey,xo,yo,f);
% c{2} = generate_2img_cost_matrix({pts_2d{2};pts_2d{3};pts_2d{1}},{trs{2};trs{3};trs{1}},[ex(2);ex(3);ex(1)],[ey(2);ey(3);ey(1)],[xo(2);xo(3);xo(1)],[yo(2);yo(3);yo(1)],[f(2);f(3);f(1)]);
% c{3} = generate_2img_cost_matrix({pts_2d{3};pts_2d{1};pts_2d{2}},{trs{3};trs{1};trs{2}},[ex(3);ex(1);ex(2)],[ey(3);ey(1);ey(2)],[xo(3);xo(1);xo(2)],[yo(3);yo(1);yo(2)],[f(3);f(1);f(2)]);

% combines the results of the 3 different Hungarian algorithms
for j = 1:n_t
    [tmp,n_c] = size(res{j});
    n_c = n_c-1;
    if n_c <= N
        cc = cc+1;
        mm(cc,:) = [res{j}(1:N),0];
    else
        k = n_c/N;
        nn = reshape(res{j}(1:n_c),N,k)';
        if k < 7
            bb = sm_comb_all(nn);
            cost = sm_comb_all_cost2(bb,pt,tr,ex,ey,xo,yo,f);
            if k <= 5
                mm(cc+1:cc+k,:) = [sm_combset_match(cost,k,bb),zeros(k,1)];
            else
                mm(cc+1:cc+k,:) = [sm_combset_match_bypart(cost,k,5,bb),zeros(k,1)];
            end
        else
            mm(cc+1:cc+k,:) = sm_match_special2(nn,c,pt,tr,ex,ey,xo,yo,f);
        end
        cc = cc+k;
    end
end

% calculate reconstruction accuracy
cost = zeros(n_seeds,1);
M = zeros(n_seeds,N+1);
M(mm(1:n_seeds,1),:) = mm(1:n_seeds,:);
pt_2d = cell(N,1);
for i = 1:n_seeds
    for j = 1:N
        pt_2d{j} = pt{j}(M(i,j),:);
    end
    [tmp,cost(i)] = sm_recon_one2(pt_2d,tr,M(i,4),ex,ey,xo,yo,f);
end

[r,matched,unmatched,m_truth] = sm_matchingrate2(M);
[n_m,tmp] = size(matched);
[n_u,tmp] = size(unmatched);
[n,tmp] = size(M);
M = [matched;unmatched];

M
r

xy = {pts_2d{1}(M(:,1),:);pts_2d{2}(M(:,2),:);pts_2d{3}(M(:,3),:)};
% pointssize = size(xy{1})
[pts_3d,ra] = sm_recon_general_2({pts_2d{1}(M(:,1),:);pts_2d{2}(M(:,2),:);pts_2d{3}(M(:,3),:)},trs,ex,ey,xo,yo,f);

% pts_2d{1}(M(:,1),:);pts_2d{2}(M(:,2),:);pts_2d{3}(M(:,3),:)
% pts_2d(1:3)


[pts_3d,ra] = sm_recon_general_2(pts_2d(1:3),trs,ex,ey,xo,yo,f);
% d3_row = sortrows(pts_3d)
% truth_row = sortrows(pts_3d_truth)
% statistics
ra_mean = mean(ra);
ra_std = std(ra);
ra_min = min(ra);
ra_max = max(ra);
res_ra = [ra_mean,ra_std,ra_min,ra_max];
ra_m = ra(1:n_m,:);
if n_u>0
  ra_u = ra(n_m+1:n,:);
else
  ra_u = 0;
end
ra_mean_m = mean(ra_m);
ra_std_m = std(ra_m);
ra_min_m = min(ra_m);
ra_max_m = max(ra_m);
res_ra_m = [ra_mean_m,ra_std_m,ra_min_m,ra_max_m];
ra_mean_u = mean(ra_u);
ra_std_u = std(ra_u);
ra_min_u = min(ra_u);
ra_max_u = max(ra_u);
res_ra_u = [ra_mean_u,ra_std_u,ra_min_u,ra_max_u];

bpe_all = [];
bpe_all_m = [];
bpe_all_u = [];
for i = 1:3
  p2d = sm_backproj_general(pts_3d,trs{i},ex(i),ey(i),xo(i),yo(i),f(i));
  [bpe_mean,bpe_std,bpe_min,bpe_max,bpe] = sm_err_backproj_general(p2d,pts_2d{i},ex(i),ey(i));
  bpe_m = bpe(1:n_m,:);
  if n_u > 0
    bpe_u = bpe(n_m+1:n,:);
  else
    bpe_u = 0;
  end
  bpe_all = [bpe_all;bpe];
  bpe_all_m = [bpe_all_m;bpe_m];
  bpe_all_u = [bpe_all_u;bpe_u];
end
bpe_mean = mean(bpe);
bpe_std = std(bpe);
bpe_min = min(bpe);
bpe_max = max(bpe);
res_bpe = [bpe_mean,bpe_std,bpe_min,bpe_max];
bpe_mean_m = mean(bpe_m);
bpe_std_m = std(bpe_m);
bpe_min_m = min(bpe_m);
bpe_max_m = max(bpe_m);
res_bpe_m = [bpe_mean_m,bpe_std_m,bpe_min_m,bpe_max_m];
bpe_mean_u = mean(bpe_u);
bpe_std_u = std(bpe_u);
bpe_min_u = min(bpe_u);
bpe_max_u = max(bpe_u);
res_bpe_u = [bpe_mean_u,bpe_std_u,bpe_min_u,bpe_max_u];

[re_mean,re_std,re_min,re_max,re,rv] = sm_err_recon(pts_3d,pts_3d_truth);
res_re = [re_mean,re_std,re_min,re_max];
re_m = re(1:n_m,:);
if n_u > 0
  re_u = re(n_m+1:n,:);
else
  re_u = 0;
end
re_mean_m = mean(re_m);
re_std_m = std(re_m);
re_min_m = min(re_m);
re_max_m = max(re_m);
res_re_m = [re_mean_m,re_std_m,re_min_m,re_max_m];
re_mean_u = mean(re_u);
re_std_u = std(re_u);
re_min_u = min(re_u);
re_max_u = max(re_u);
res_re_u = [re_mean_u,re_std_u,re_min_u,re_max_u];

regtr = sm_regtrans(pts_3d,pts_3d_truth(rv,:));
pts_3dnew = sm_regmapping(pts_3d,regtr);
[ren_mean,ren_std,ren_min,ren_max,ren] = sm_err_recon(pts_3dnew,pts_3d_truth);
res_ren = [ren_mean,ren_std,ren_min,ren_max];
ren_m = ren(1:n_m,:);
if n_u > 0
  ren_u = ren(n_m+1:n,:);
else
  ren_u = 0;
end
ren_mean_m = mean(ren_m);
ren_std_m = std(ren_m);
ren_min_m = min(ren_m);
ren_max_m = max(ren_m);
res_ren_m = [ren_mean_m,ren_std_m,ren_min_m,ren_max_m];
ren_mean_u = mean(ren_u);
ren_std_u = std(ren_u);
ren_min_u = min(ren_u);
ren_max_u = max(ren_u);
res_ren_u = [ren_mean_u,ren_std_u,ren_min_u,ren_max_u];

res_re
% r



% profile report